import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:mokeng/values/values.dart';

class HomeController extends GetxController {
  /// 响应式成员变量
  // 当前 tab 页码
  final _page = 0.obs;

  set page(value) => _page.value = value;

  get page => _page.value;

  /// 成员变量
  // tab 页标题
  late final List<String> tabTitles;

  // 页控制器
  late final PageController pageController;

  // 底部导航项目
  late final List<BottomNavigationBarItem> bottomTabs;

  DateTime? lastBackPressed;

  /// 事件
  // tab栏动画
  void handleNavBarTap(int index) {
    pageController.animateToPage(index,
        duration: const Duration(milliseconds: 200), curve: Curves.ease);
  }

  // tab栏页码切换
  void handlePageChanged(int page) {
    this.page = page;
  }

  /// 生命周期
  @override
  void onInit() {
    super.onInit();
    // 准备一些静态数据
    tabTitles = ['交易'.tr, '发现'.tr, '商城'.tr, '我的'.tr];
    bottomTabs = <BottomNavigationBarItem>[
       BottomNavigationBarItem(
        icon: Image.asset("assets/images/ic_tab_trading_nor.webp"),
        activeIcon: Image.asset("assets/images/ic_tab_trading_pre.webp"),
        label: tabTitles[0],
        backgroundColor: AppColors.scaffoldBackground,
      ),
      BottomNavigationBarItem(
        icon: Image.asset("assets/images/ic_tab_community_nor.webp"),
        activeIcon: Image.asset("assets/images/ic_tab_community_pre.webp"),
        label: tabTitles[1],
        backgroundColor: AppColors.scaffoldBackground,
      ),
      BottomNavigationBarItem(
        icon: Image.asset("assets/images/ic_tab_store_nor.webp"),
        activeIcon: Image.asset("assets/images/ic_tab_store_pre.webp"),
        label: tabTitles[2],
        backgroundColor: AppColors.scaffoldBackground,
      ),
      BottomNavigationBarItem(
        icon: Image.asset("assets/images/ic_tab_mine_nor.webp"),
        activeIcon: Image.asset("assets/images/ic_tab_mine_pre.webp"),
        label: tabTitles[3],
        backgroundColor: AppColors.scaffoldBackground,
      ),
    ];
    pageController = PageController(initialPage: page);
  }

  @override
  void dispose() {
    // uriSub?.cancel();
    pageController.dispose();
    super.dispose();
  }
}
